package org.hako.recite.word.test.impl.gui.swing;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.hako.recite.word.Constants;
import org.hako.recite.word.gui.swing.AbstractReciteWordFrame;
import org.hako.recite.word.test.impl.gui.NullWordTestBehavior;
import org.hako.recite.word.test.impl.gui.WordTestBehavior;

/**
 * Home frame.
 * 
 * @since 0.4.6
 * @author <a href="mailto:xnnyygn@gmail.com">xnnyygn</a>
 */
public class HomeFrame extends AbstractReciteWordFrame {

	private WordTestBehavior behavior;

	/**
	 * Create
	 */
	public HomeFrame() {
		this(new NullWordTestBehavior());
	}

	/**
	 * Create
	 * 
	 * @param behavior
	 */
	public HomeFrame(WordTestBehavior behavior) {
		super(Constants.APPLICATION_TITLE);
		this.behavior = behavior;
		exitOnClose();
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		frame.add(panel);
		panel.setPreferredSize(new Dimension(200, panel.getSize().height));
		addComponentsToPane(panel);
		frame.setSize(400, 300);
		frame.setResizable(false);
		
		// center in the monitor
		centerInScreen();
	}

	/**
	 * Add components to pane.
	 * 
	 * @param container
	 */
	private void addComponentsToPane(Container container) {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		JButton btnStart = new JButton("Start Word Test");
		btnStart.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				behavior.showTestSettingWindow();
			}
		});
		container.add(btnStart, c);
		c.insets = new Insets(5, 0, 0, 0);
		c.gridy = 1;
		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		container.add(btnExit, c);
	}
}
